$: << File.join(File.expand_path(File.dirname(__FILE__)), "..")

require 'rubygems'
require 'optparse'
require 'EasyHadoop'

#==================================
# ColExtractor.rb
# Extract particular columns from the datasets
#==================================
class ColExtractor < EasyHadoop
	attr_reader :key, :value
	
	def initialize(arguments)
		super(arguments)
		@max_index = (@options[:key] + (@options[:value] ||[])).sort.last
		@key = nil
		@value = []
	end
	
	def set_mandatory
		super
		@mandatory << :key
		@mandatory << :key_delimiter
		@mandatory << :value_delimiter
	end
	
	def set_commands
		super
		@optparse.banner = "Usage: ruby #{File.basename(__FILE__)} --key COMMA_LIST [OPTIONS]"
		
		@options[:key] = nil
		@optparse.on('-k','--key LIST', 'key columns. Separate multiple columns by comma. By default they are separated using pipe character. Use --key_delimiter to define different delimiter') do |keys|
			@options[:key] = OptionParserUtil.parse_int_array(keys)
		end

		@options[:key_delimiter]='|'
		@optparse.on('--key-delimiter DELM', 'Separator to use while joining key columns') do |sep|
			@options[:key_delimiter]=OptionParserUtil.handle_delimiter(sep, '|') 
		end
		
		@options[:value]=nil
		@optparse.on('-v','--value 1,2,3', 'value columns. separate multiple columns by comma. By default they are separated using tab character. Use --value_delimiter to define different delimiter') do |index|
			@options[:value] = OptionParserUtil.parse_int_array(index)
		end

		@options[:value_delimiter]="\t"
		@optparse.on('--value-delimiter DELM','Delimiter between value columns. By default its tab') do |sep|
			@options[:value_delimiter]=OptionParserUtil.handle_delimiter(sep, "\t")
		end
	end
	
	
	def execute(line)
		
		return line if line.nil? or line.chomp.empty?
		
		tokens = line.to_s.chomp.split(@options[:input_delimiter])
		
		#Sanity Check
		if tokens.length-1 < @max_index 
			return nil if @options[:ignore]
			raise Exception, "Expecting atleast #{@max_index+1} columns. Found #{tokens.length}"
		end
		
		#extract keys
		key = []
		@options[:key].each{|k|
			key << tokens[k]
		}
		
		#extract values
		value = []
		@options[:value].each{|v| value << tokens[v] } unless @options[:value].nil?
		return "#{key.join(@options[:key_delimiter])}\t#{value.join(@options[:value_delimiter])}"	
	end
end

# Scripting Option
if __FILE__ == $0
	cls = ColExtractor.new(ARGV)
    STDIN.each_line{|line|
	  value = cls.execute(line)
      puts value unless value.nil?
    }
end
